package butler.dl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SQLHelper {
	private static SQLiteDatabase db;

	public SQLHelper(final Context context) {
		if (db == null)
			synchronized (SQLHelper.class) {
				if (db == null) {
					db = new ButlerDatabase(context).getWritableDatabase();
				}
			}
	}

	public boolean AddAccount(final Account account) {
		ContentValues values = new ContentValues();
		values.put("AccountID", this.getMaxAccountID());
		values.put("AccountClass", account.getAccountClass());
		values.put("Subclass", account.getSubclass());
		values.put("Deletion", account.getDeletionFlag());
		if (db.insert("Account", null, values) > 0) {
			return true;
		} else {
			return false;
		}
	}

	private Integer getMaxAccountID() {
		Integer maxID = 0;
		Cursor cur = db.rawQuery(
				"select max(AccountID) as maxAccountID from Account", null);
		if (cur.moveToFirst()) {
			maxID = cur.getInt(cur.getColumnIndex("AccountID"));
		}

		return maxID;

	}

}
